﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcEngine.Mvc.ModelBinders;
using MvcEngine.Gallery.Models;
using MvcEngine.Core;
using System.Web;
using System.Web.Mvc;
using MvcEngine.Core.Localization;

namespace MvcEngine.Gallery.ModelBinders
{
    public class GalleryImageModelBinder : ModelBinderBase
    {
        public override object Bind(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            HttpRequestBase request = controllerContext.HttpContext.Request;
            GalleryImage image = IoC.Resolve<GalleryImage>();

            string imageType = request.Form.Get("ImageType");
            if (imageType == GalleryImageTypes.Url)
            {
                image.Url = request.Form.Get("Url");
                image.Type = GalleryImageTypes.Url;
            }
            else 
            {
                image.Type = GalleryImageTypes.Physical;
            }

            image.Name = request.Form.Get("Name");
            image.Description = request.Form.Get("Description");
            

            image.Visible = request.Form.Get("Visible").Contains("true");

            Validate(image, controllerContext);
            return image;
        }
    }
}
